package org.mms.controller;

import org.mms.pojo.dto.MaterialDto;
import org.mms.pojo.entity.Material;
import org.mms.pojo.request.MaterialRequest;
import org.mms.pojo.util.ResponseMessage;
import org.mms.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/materials")
public class MaterialController {
    @Autowired
    private MaterialService materialService;

    @GetMapping
    public ResponseMessage<List<MaterialDto>> list() {
        return ResponseMessage.success(materialService.list()
                .stream()
                .map(Material::convert)
                .toList());
    }

    @GetMapping("/{id}")
    public ResponseMessage<MaterialDto> getById(@PathVariable Long id) {
        return ResponseMessage.success(materialService.getById(id)
                .convert());
    }

    @PostMapping
    public ResponseMessage<MaterialDto> create(@RequestBody MaterialRequest request) {
        return ResponseMessage.success(materialService.create(request)
                .convert());
    }

    @PutMapping
    public ResponseMessage<MaterialDto> update(@RequestBody MaterialRequest request) {
        return ResponseMessage.success(materialService.update(request)
                .convert());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<MaterialDto> delete(@PathVariable Long id) {
        materialService.delete(id);
        return ResponseMessage.success(null);
    }
}